package com.movitech.mobile.cz.base.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.IdGenUtils;
import com.movitech.mobile.cz.base.common.utils.StringUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizvo.MessageTemplateRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SMessageTemplate;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSMessageTemplate;
import com.movitech.mobile.cz.base.sys.mapper.SMessageTemplateMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSMessageTemplateMapper;
import com.movitech.mobile.cz.base.sys.service.SMessageTemplateService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息模板 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sMessageTemplateService")
@Transactional
public class SMessageTemplateServiceImpl extends ServiceImpl<SMessageTemplateMapper, SMessageTemplate> implements SMessageTemplateService {

	@Autowired
	private ExtSMessageTemplateMapper extSMessageTemplateMapper;
	
	@Override
	public RestReturnResult<Object> saveOrUpdateMessageTemplate(MessageTemplateRequestVO messageTemplateRequestVO) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		SMessageTemplate messageTemplate = null;
		if(messageTemplateRequestVO != null){
			
			if(StringUtils.isNotEmpty(messageTemplateRequestVO.getId())){ //update
				
				messageTemplate = baseMapper.selectById(messageTemplateRequestVO.getId());
				
				if(messageTemplate != null){
					
					messageTemplate.setMsgType(messageTemplateRequestVO.getMsgType());
					messageTemplate.setMsgName(messageTemplateRequestVO.getMsgName());
					messageTemplate.setMsgContent(messageTemplateRequestVO.getMsgContent());
					messageTemplate.setMsgVariables(messageTemplateRequestVO.getMsgVariables());
					
					Date updateDate = new Date();
					String userId = UserUtils.getCurrentUserId();
					messageTemplate.setUpdateBy(userId);
					messageTemplate.setUpdateDate(updateDate);
					messageTemplate.setDelFlag(CommonConstants.NO_INTEGER);
					
					baseMapper.updateById(messageTemplate);
					
					hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[1], CommonConstants.SUCCESS_DESCS[1]);
					
				}else{ //数据不存在
					hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[2], CommonConstants.ERROR_DESCS[2]);
				}
				
			}else{ //insert
				
				messageTemplate = new SMessageTemplate();
				messageTemplate.setMsgType(messageTemplateRequestVO.getMsgType());
				messageTemplate.setMsgName(messageTemplateRequestVO.getMsgName());
				messageTemplate.setMsgContent(messageTemplateRequestVO.getMsgContent());
				messageTemplate.setMsgVariables(messageTemplateRequestVO.getMsgVariables());
				
				Date createDate = new Date();
				String userId = UserUtils.getCurrentUserId();
				messageTemplate.setId(IdGenUtils.uuid());
				messageTemplate.setMsgStatus(CommonConstants.YES_INTEGER);
				messageTemplate.setCreateBy(userId);
				messageTemplate.setCreateDate(createDate);
				messageTemplate.setUpdateBy(userId);
				messageTemplate.setUpdateDate(createDate);
				messageTemplate.setDelFlag(CommonConstants.NO_INTEGER);
				
				baseMapper.insert(messageTemplate);
				
				hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[0], CommonConstants.SUCCESS_DESCS[0]);
			}
			
		}else{//请求数据出错
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
		}
		
		return hResult;
	}

	@Override
	public RestReturnResult<Object> updateMessageTemplateForDel(String id) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		if(StringUtils.isNotEmpty(id)){
			
			SMessageTemplate messageTemplate = baseMapper.selectById(id);
			
			if(messageTemplate != null){
				
				messageTemplate.setDelFlag(CommonConstants.YES_INTEGER);
				
				Date updateDate = new Date();
				String userId = UserUtils.getCurrentUserId();
				messageTemplate.setUpdateBy(userId);
				messageTemplate.setUpdateDate(updateDate);
				
				baseMapper.updateById(messageTemplate);
				
				hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[2], CommonConstants.SUCCESS_DESCS[2]);
				
			}else{ //数据不存在
				hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[2], CommonConstants.ERROR_DESCS[2]);
			}
			
		}else{
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
		}
		
		return hResult;
	}

	@Override
	public RestReturnResult<Object> updateMessageTemplateStatus(MessageTemplateRequestVO messageTemplateRequestVO) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		if(messageTemplateRequestVO != null){
			
			if(StringUtils.isNotEmpty(messageTemplateRequestVO.getId())){
				
				SMessageTemplate messageTemplate = baseMapper.selectById(messageTemplateRequestVO.getId());
				if(messageTemplate != null){
					messageTemplate.setMsgStatus(messageTemplateRequestVO.getMsgStatus());
					
					Date updateDate = new Date();
					String userId = UserUtils.getCurrentUserId();
					messageTemplate.setUpdateBy(userId);
					messageTemplate.setUpdateDate(updateDate);
					
					baseMapper.updateById(messageTemplate);
					
					if(CommonConstants.YES_INTEGER == messageTemplateRequestVO.getMsgStatus()){//启用
						hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[3], CommonConstants.SUCCESS_DESCS[3]);
					}else{//停用
						hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[4], CommonConstants.SUCCESS_DESCS[4]);
					}
					
				}else{//数据不存在
					hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[2], CommonConstants.ERROR_DESCS[2]);
				}
				
			}else{
				hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
			}
			
		}else{//请求数据出错
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
		}
		
		return hResult;
	}

	@Override
	public List<ExtSMessageTemplate> getNoticeListByShowNum(Map<String, Object> map) {
		return extSMessageTemplateMapper.getNoticeListByShowNum(map);
	}

	@Override
	public SMessageTemplate getByMsgType(int type) {
		SMessageTemplate sMessageTemplate = new SMessageTemplate();
		EntityWrapper entityWrapper = new EntityWrapper();
		sMessageTemplate.setMsgType(type);
		sMessageTemplate.setMsgStatus(CommonConstants.MSG_TEMPLATE_OPEN);
		sMessageTemplate.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
		entityWrapper.setEntity(sMessageTemplate);
		return selectOne(entityWrapper);
	}

}
